<template>
  <div class="page">
    <header class="flex">
      <router-link tag="i" to="./city-list">
        <van-icon name="arrow-left" color="#ffffff" />
      </router-link>
      <van-field v-model="value" />
      <router-link tag="i" to="./city-list">
        <button>取消</button>
      </router-link>
    </header>
    <div class="list-box">
      <ul>
        <li v-for="item in cityList" :key="item.id" @click="addCity(item)">
          {{ item.ReadmeName }}
        </li>
      </ul>
      <p v-if="cityList.length == 0">未找到结果</p>
    </div>
  </div>
</template>

<script>
import { ref, computed, onActivated } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import Citys from "../config/citys";
import _ from "lodash";
export default {
  name: "AddCity",
  setup() {
    const router = useRouter();
    const store = useStore();
    const value = ref("");

    const cityList = computed(() => {
      return _.filter(Citys, (item) => {
        return (
          item.ReadmeName.indexOf(value.value) > -1 ||
          value.value.indexOf(item.ReadmeName) > -1
        );
      });
    });

    onActivated(() => {
      value.value = "";
    });

    const addCity = (data) => {
      store.commit("addCity", data);
      router.push("/city-list");
    };

    return {
      value,
      cityList,
      addCity,
    };
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 100vw;
  max-width: 750px;
  height: 100vh;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-image: url(../assets/images/bg2.jpg);
  overflow: hidden;

  header {
    width: 100%;
    height: 60px;
    background-color: rgba($color: #000000, $alpha: 0.3);
    color: #ffffff;
    font-size: 20px;
    align-items: center;
    padding: 0 5px;
    position: fixed;
    left: 0;
    top: 0;
    box-sizing: border-box;
    i {
      font-size: 30px;
    }

    .van-field {
      margin: 0 10px;
      flex: 1;
      border-radius: 5px;
      height: 30px;
      line-height: 30px;
      padding: 0 0 0 10px;
      font-size: 20px;
    }
    button {
      background: none;
      font-size: 16px;
      color: #ffffff;
      border: 2px solid #ffffff;
      padding: 2px 10px;
      box-sizing: border-box;
      border-radius: 5px;
    }
  }
  .list-box {
    width: 100%;
    margin-top: 60px;
    height: calc(100vh - 60px);
    overflow: scroll;
    text-align: left;
    box-sizing: border-box;

    ul > li {
      font-size: 16px;
      color: #ffffff;
      line-height: 40px;
      background-color: rgba($color: #000000, $alpha: 0.2);
      margin-top: 3px;
      padding: 0 45px;
    }

    > p {
      margin-top: 3px;
      font-size: 16px;
      color: #ffffff;
      line-height: 40px;
      padding: 0 45px;
      background-color: rgba($color: #000000, $alpha: 0.2);
    }
  }
}
</style>